package fifis.base.controller;

import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.GhostControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;
import fifis.game.common.Common;
import fifis.mvc.controller.FController;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.scene.Node;
import org.apache.log4j.Logger;

/**
 *
 * @author porfiriopartida
 */
public abstract class BController extends FController {
    protected final static Logger log4j = Logger.getLogger(BController.class);
    static final long serialVersionUID = 1L;

    protected CollisionShape shape;
    protected GhostControl ghostControl;
    protected RigidBodyControl rigidBodyControl;
    protected CharacterControl player;

    public void createCharacterControl() {
        shape = getCharacterCollisionShape();
        player = new CharacterControl(shape, 0.01f);
        getNode().addControl(player);
        player.setGravity(0.00f);
        Common.getInstance().getBulletAppState().getPhysicsSpace().add(player);

//        BoundingVolume bound = new BoundingSphere(cShape.getRadius(), player.getPhysicsLocation());
//        getNode().setModelBound(bound);
//        getNode().updateModelBound();
    }

    public abstract CollisionShape getCharacterCollisionShape();

    public abstract CollisionShape getGhostCollisionShape();

    public abstract CollisionShape getRigidCollisionShape();
    

    public void setLocalTranslation(Vector3f position) {
        if (player != null) {
            player.setPhysicsLocation(position);
            return;
        }
        getNode().setLocalTranslation(position);
    }

    public GhostControl getGhostControl() {
        return ghostControl;
    }

    public void setGhostControl(GhostControl ghostControl) {
        this.ghostControl = ghostControl;
    }

    public RigidBodyControl getRigidBodyControl() {
        return rigidBodyControl;
    }

    public void setRigidBodyControl(RigidBodyControl rigidBodyControl) {
        this.rigidBodyControl = rigidBodyControl;
    }

    public void createRigidControl() {
        createRigidControl(Vector3f.UNIT_Y.negate(), 1);
    }

    public void createRigidControl(Vector3f gravity, float mass) {
        Spatial sceneModel = getNode();
//        CollisionShape sceneShape =
//                CollisionShapeFactory.createDynamicMeshShape(sceneModel);
//        CollisionShape sceneShape =
//                new CapsuleCollisionShape(2, 3);
        rigidBodyControl = new RigidBodyControl(getRigidCollisionShape(), mass);
        sceneModel.addControl(rigidBodyControl);
//        rigidBodyControl.setGravity(Vector3f.UNIT_Y.negate());
        Common.getInstance().getBulletAppState().getPhysicsSpace().add(rigidBodyControl);
    }
    public String getGhostName(){
        return getName()+"ghostNode";
    }
    public void createGhostControl() {

//        shape = CollisionShapeFactory.createDynamicMeshShape(getNode());
//        ghostControl = new GhostControl(shape);
//        shape = CollisionShapeFactory.createDynamicMeshShape(getNode());
        ghostControl = new GhostControl(getGhostCollisionShape());
        Node ghostNode = new Node(getGhostName());
        ghostNode.addControl(ghostControl);
        getNode().attachChild(ghostNode);
//        Common.getInstance().getRootNode().attachChild(ghostControl.debugShape());
//         Common.getInstance().getBulletAppState().get

//        Common.getInstance().getBulletAppState().getPhysicsSpace().addCollisionListener(listener);
        Common.getInstance().getBulletAppState().getPhysicsSpace().add(ghostControl);
    }
//        Common.getInstance().getBulletAppState().getPhysicsSpace().addCollisionListener(this);
//        Common.getInstance().getBulletAppState().getPhysicsSpace().add(ghost);
//        public BController(){
//        
//        }
//        CollisionShape shape = CollisionShapeFactory.createDynamicMeshShape(view.getNode());
//        GhostControl ghost = new GhostControl(shape);
//        
//        Common.getInstance().getBulletAppState().getPhysicsSpace().addCollisionListener(this);
//        Common.getInstance().getBulletAppState().getPhysicsSpace().add(ghost);
//        public BController(){
//        
//        }
//    private BModel model;
//    private BView view;
//    public BController(BModel model, BView view){
////        this.model = model;
////        this.view = view;
//        super(model, view);
//        log4j.debug(getModel());
//    }
//    public BController(){
//        super();
//    }
//    @Override
//    public void setModel(FModel model) {
//        if(model instanceof BModel){ super.setModel(model); }
//        else throw new IllegalArgumentException("BModel spected but " + model.getClass() + " received.");
////        this.model = (BModel) model;
//    }
//    @Override
//    public void setView(FView view) {
//        if(view instanceof BView){ super.setView(view); }
//        else throw new IllegalArgumentException("FView spected but " + view.getClass() + " received.");
////        this.view = (BView) view;
//    }
//    public void setModel(BModel model) {
//        this.model = model;
//    }
//    public void setView(BView view) {
//        this.view = view;
//    }
//    public BModel getBModel() {
//        return model;
//    }
//    
//    public BView getBView() {
//        return view;
//    }
}
